<?php

class StatReader {
	
	var $char;
	var $class_talents = array(
		"Druid" => array("Balance", "Feral Combat", "Restoration"),
		"Hunter" => array("Beast Mastery", "Marksmanship", "Survival"),
		"Mage" => array("Arcane", "Fire", "Frost"),
		"Paladin" => array("Holy", "Protection", "Retribution"),
		"Priest" => array("Discipline", "Holy", "Shadow"),
		"Rogue" => array("Assassination", "Combat", "Subtlety"),
		"Shaman" => array("Elemental", "Enhancement", "Restoration"),
		"Warlock" => array("Affliction", "Demonology", "Destruction"),
		"Warrior" => array("Arms", "Fury", "Protection")
	);
	
	function setCharObject($char) {
		$this->char = $char;
	}
	
	function getStat($stat) {
		switch ($stat) {
			case '<empty>':
				return '';
			case 'Level':
				return $this->char->getCharacter('level');
			case 'Gender ID':
				return $this->char->getCharacter('genderid');
			case 'Race ID':
				return $this->char->getCharacter('raceid');
			case 'Race':
				return $this->char->getCharacter('race');
			case 'Class ID':
				return $this->char->getCharacter('classid');
			case 'Class':
				return $this->char->getCharacter('class');
			case 'Name':
				return $this->char->getCharacter('name');
			case 'Guild':
				return $this->char->getCharacter('guildname');
			case 'Server':
				return $this->char->getCharacter('realm');
			case 'HKs':
				$pvp = $this->char->getCharacterTab('pvp');
				return $pvp[0]['children']['lifetimehonorablekills'][0]['attr']['value'];
			case 'Talents':
				return $this->char->getCharacterTab('talentspec');
			case 'Title':
				$title = $this->char->getCharacterTab('title');
				return $title[0]['attr']['value'];
			case 'Bonus Healing':
				$spell = $this->char->getCharacterTab('spell');
				return $spell[0]['children']['bonushealing'][0]['attr']['value'];
			case 'Spell Crit Chance':
				$spell_types = array("fire", "frost", "holy", "nature", "shadow");
				$spell = $this->char->getCharacterTab('spell');
				$spellcrit = 0;
				foreach ($spell_types as $spell_type) {
					if ($spell[0]['children']['critchance'][0]['children'][$spell_type][0]['attr']['percent'] > $spellcrit) {
						$spellcrit = $spell[0]['children']['critchance'][0]['children'][$spell_type][0]['attr']['percent'];
					}
				}
				return $spellcrit.'%';
			case 'Spell Hit':
				$spell = $this->char->getCharacterTab('spell');
				return $spell[0]['children']['hitrating'][0]['attr']['value'];
			case 'Bonus Damage':
				$spell = $this->char->getCharacterTab('spell');
				$maxplusdam = 0;
				$spell_types = array("fire", "frost", "holy", "nature", "shadow");
				foreach ($spell_types as $spell_type) {
					if ($spell[0]['children']['bonusdamage'][0]['children'][$spell_type][0]['attr']['value'] > $maxplusdam) {
						$maxplusdam = $spell[0]['children']['bonusdamage'][0]['children'][$spell_type][0]['attr']['value'];
					}
				}
				return $maxplusdam;
			case 'Bonus Damage Type':
				$spell = $this->char->getCharacterTab('spell');
				$maxplusdam = 0;
				$maxplustype = '';
				$spell_types = array('fire', 'frost', 'holy', 'nature', 'shadow');
				foreach ($spell_types as $spell_type) {
					if ($spell[0]['children']['bonusdamage'][0]['children'][$spell_type][0]['attr']['value'] > $maxplusdam) {
						$maxplusdam = $spell[0]['children']['bonusdamage'][0]['children'][$spell_type][0]['attr']['value'];
						$maxplustype = ucfirst($spell_type);
					} else if ($spell[0]['children']['bonusdamage'][0]['children'][$spell_type][0]['attr']['value'] == $maxplusdam) {
						$maxplustype = '';
					}
				}
				return ($maxplustype == '') ? 'Bonus Damage' : 'Bonus '.$maxplustype.' Damage';
			case 'Mana Regen':
				$spell = $this->char->getCharacterTab('spell');
				return round($spell[0]['children']['manaregen'][0]['attr']['notcasting']);
			case 'Mana Regen2':
				$spell = $this->char->getCharacterTab('spell');
				return round($spell[0]['children']['manaregen'][0]['attr']['notcasting']).'/'.round($spell[0]['children']['manaregen'][0]['attr']['casting']);
			case 'Penetration':
				$spell = $this->char->getCharacterTab('spell');
				return round($spell[0]['children']['penetration'][0]['attr']['value']);
			case 'Health':
				$charbars = $this->char->getCharacterTab('characterbars');
				return $charbars[0]['children']['health'][0]['attr']['effective'];
			case 'Secondbar':
				$charbars = $this->char->getCharacterTab('characterbars');
				return $charbars[0]['children']['secondbar'][0]['attr']['effective'];
			case 'Secondbar Type':
				$charbars = $this->char->getCharacterTab('characterbars');
				switch ($charbars[0]['children']['secondbar'][0]['attr']['type']) {
					case 'r':
						return 'Rage';
					case 'e':
						return 'Energy';
					default:
						return 'Mana';
				}
			case 'Strength':
				$basestats = $this->char->getCharacterTab('basestats');
				return $basestats[0]['children']['strength'][0]['attr']['effective'];
			case 'Agility':
				$basestats = $this->char->getCharacterTab('basestats');
				return $basestats[0]['children']['agility'][0]['attr']['effective'];
			case 'Stamina':
				$basestats = $this->char->getCharacterTab('basestats');
				return $basestats[0]['children']['stamina'][0]['attr']['effective'];
			case 'Intellect':
				$basestats = $this->char->getCharacterTab('basestats');
				return $basestats[0]['children']['intellect'][0]['attr']['effective'];
			case 'Spirit':
				$basestats = $this->char->getCharacterTab('basestats');
				return $basestats[0]['children']['spirit'][0]['attr']['effective'];
			case 'Armor':
				$basestats = $this->char->getCharacterTab('basestats');
				return $basestats[0]['children']['armor'][0]['attr']['effective'];
			case 'Ranged DPS':
				$rangedstats = $this->char->getCharacterTab('ranged');
				return $rangedstats[0]['children']['damage'][0]['attr']['dps'];
			case 'Ranged Hit Chance':
				$rangedstats = $this->char->getCharacterTab('ranged');
				return $rangedstats[0]['children']['hitrating'][0]['attr']['increasedhitpercent'].'%';
			case 'Ranged Crit Chance':
				$rangedstats = $this->char->getCharacterTab('ranged');
				return $rangedstats[0]['children']['critchance'][0]['attr']['percent'].'%';
			case 'Ranged Attack Power':
				$rangedstats = $this->char->getCharacterTab('ranged');
				return $rangedstats[0]['children']['power'][0]['attr']['effective'];
			case 'Melee DPS':
				$meleestats = $this->char->getCharacterTab('melee');
				return $meleestats[0]['children']['mainhanddamage'][0]['attr']['dps'];
			case 'Melee Crit Chance':
				$meleestats = $this->char->getCharacterTab('melee');
				return $meleestats[0]['children']['critchance'][0]['attr']['percent'].'%';
			case 'Melee Hit Chance':
				$meleestats = $this->char->getCharacterTab('melee');
				return $meleestats[0]['children']['hitrating'][0]['attr']['increasedhitpercent'].'%';
			case 'Melee Attack Power':
				$meleestats = $this->char->getCharacterTab('melee');
				return $meleestats[0]['children']['power'][0]['attr']['effective'];
			case 'Defense':
				$defensestats = $this->char->getCharacterTab('defenses');
				return $defensestats[0]['children']['defense'][0]['attr']['value']+$defensestats[0]['children']['defense'][0]['attr']['plusdefense'];
			case 'Parry':
				$defensestats = $this->char->getCharacterTab('defenses');
				return $defensestats[0]['children']['parry'][0]['attr']['percent'].'%';
			case 'Resilience':
				$defensestats = $this->char->getCharacterTab('defenses');
				return round($defensestats[0]['children']['resilience'][0]['attr']['value']);
			case 'Dodge':
				$defensestats = $this->char->getCharacterTab('defenses');
				return $defensestats[0]['children']['dodge'][0]['attr']['percent'].'%';
			case 'Block':
				$defensestats = $this->char->getCharacterTab('defenses');
				return $defensestats[0]['children']['block'][0]['attr']['percent'].'%';
			case 'Talent Tree One':
				$talents = $this->char->getCharacterTab('talentspec');
				return $talents[0]['attr']['treeone'];
			case 'Talent Tree Two':
				$talents = $this->char->getCharacterTab('talentspec');
				return $talents[0]['attr']['treetwo'];
			case 'Talent Tree Three':
				$talents = $this->char->getCharacterTab('talentspec');
				return $talents[0]['attr']['treethree'];
			case 'Build':
				$build = "Hybrid";
				$class = $this->getStat('Class');
				$talents = $this->char->getCharacterTab('talentspec');
				if ($talents[0]['attr']['treeone'] > 30) {
					$build = $this->class_talents[$class][0];
				} else if ($talents[0]['attr']['treetwo'] > 30) {
					$build = $this->class_talents[$class][1];
				} else if ($talents[0]['attr']['treethree'] > 30) {
					$build = $this->class_talents[$class][2];
				} else if ($talents[0]['attr']['treeone'] == 0 &&
									$talents[0]['attr']['treetwo'] == 0 &&
									$talents[0]['attr']['treethree'] == 0) {
					$build = "Untalented";
				}
				return $build;
			case 'Exalted Factions':
				$factions = $this->char->getReputationTab();
				$exalted = array();
				foreach($factions as $catkey => $catvalue) {
					foreach($catvalue['children']['faction'] as $factionvalue) {
						//echo $factionvalue['attr']['name'].' : '.$factionvalue['attr']['reputation']."<br />\n";
						if ($factionvalue['attr']['reputation'] >= 42000) { // exalted
							$exalted[] = array('name' => $factionvalue['attr']['name'], 'reputation' => $factionvalue['attr']['reputation']);
						}
					}
				}
				return count($exalted);
			case 'Prof1 Skill':
				$professions = $this->char->getCharacterTab('professions');
				return $professions[0]['children']['skill'][0]['attr']['value'];
			case 'Prof1 Name':
				$professions = $this->char->getCharacterTab('professions');
				return $professions[0]['children']['skill'][0]['attr']['name'];
			case 'Prof2 Skill':
				$professions = $this->char->getCharacterTab('professions');
				return $professions[0]['children']['skill'][1]['attr']['value'];
			case 'Prof2 Name':
				$professions = $this->char->getCharacterTab('professions');
				return $professions[0]['children']['skill'][1]['attr']['name'];
			case '5v5 Rating':
				$arenateams = $this->char->getArena();
				if (count($arenateams) > 0) {
					foreach ($arenateams as $arenateam) {
						if ($arenateam['attr']['size'] == 5) {
							return $arenateam['attr']['rating'];
						}
					}
				}
				return 'N/A';
			case '3v3 Rating':
				$arenateams = $this->char->getArena();
				if (count($arenateams) > 0) {
					foreach ($arenateams as $arenateam) {
						if ($arenateam['attr']['size'] == 3) {
							return $arenateam['attr']['rating'];
						}
					}
				}
				return 'N/A';
			case '2v2 Rating':
				$arenateams = $this->char->getArena();
				if (count($arenateams) > 0) {
					foreach ($arenateams as $arenateam) {
						if ($arenateam['attr']['size'] == 2) {
							return $arenateam['attr']['rating'];
						}
					}
				}
				return 'N/A';
			default:
				return 'NOCASE!';
		}
	}
}


?>